import axios from 'axios'

const http = axios.create({
    baseURL: 'http://localhost:8000',
    timeout: 4000, // 请求超时时间
    headers: {
        'Content-Type': 'application/json'
    }
})


//  请求拦截器
http.interceptors.request.use(
    config => {
        const token = localStorage.getItem('token');
        console.log(token);
        if (token) {
            config.headers['Authorization'] = token
        }
        return config
    },
    error => {
        return Promise.reject(error)
    }
)

// 响应拦截器
http.interceptors.response.use(function (response) {
    return response;
}, function (error) {
    console.log("skdhsjdhsjdh>>>>",error.response.status)
    if (error.response.status === 401) {
        localStorage.removeItem('token')
        window.location.href = '/first_logins'
    }
    return Promise.reject(error.message);
});

export default http
